import { http } from "@/utils/http";
import type { Secret, SecretList } from "./types";
import type { BaseResponse, PromiseRes } from "@/api/response";
import {
  k8sPath,
  k8sPathMergeReqs,
  type MakePathReq,
  type MergePathReq,
  sourceUrl
} from "./apiHelper";

// export type SecretQuery = ListOption & {
//   namespace?: string;
//   name?: string;
// };

export class K8sSecret {
  baseReq: MakePathReq = {
    src: "secrets",
    app: ""
  };

  getSecret = (query: MergePathReq): BaseResponse<SecretList> => {
    const api = k8sPath({
      ...this.baseReq,
      namespace: query.namespace
    });
    return http.get(api, { params: query });
  };

  getSecretDetail = (
    query: MergePathReq,
    data?: Secret
  ): PromiseRes<Secret> => {
    const api = k8sPathMergeReqs(query, this.baseReq, data);
    return http.get(api, { params: query.query });
  };

  updateSecret = (query: MergePathReq, data: Secret): PromiseRes => {
    const api = k8sPathMergeReqs(this.baseReq, query, data);
    return http.put(api, { data });
  };

  deleteSecret = (req: MergePathReq<Secret>): PromiseRes => {
    const api = k8sPathMergeReqs(this.baseReq, req);

    return http.delete(api, { data: req.data });
  };

  createSecret = (data: Secret, dryrun?: boolean): Promise<any> => {
    return http.post(sourceUrl("secrets", data.metadata.namespace), {
      data: { ...data, dryRun: dryrun }
    });
  };

  getImageRepoList = (): BaseResponse => {
    const api = k8sPathMergeReqs(this.baseReq);
    const params = { type: "kubernetes.io/dockerconfigjson" };
    return http.get(api, { params });
  };
}
